package com.xnyzc.lhy.mis.controller.cmsapi.worksheet;


import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.entity.WorksheetRv;
import com.xnyzc.lhy.common.exception.WorksheetException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.worksheet.WorksheetErrorCode;
import com.xnyzc.lhy.mis.entity.param.worksheet.didi.ChangePriceParam;
import com.xnyzc.lhy.mis.entity.param.worksheet.softbar.HeliPhoneUrlResult;
import com.xnyzc.lhy.mis.entity.param.worksheet.softbar.SoftPhoneUserInfoResult;
import com.xnyzc.lhy.mis.entity.worksheet.OaSysWorksheetOperate;
import com.xnyzc.lhy.mis.service.worksheet.IOaSysWorksheetOperateService;
import com.xnyzc.lhy.mis.service.worksheet.IOaSysWorksheetService;
import com.xnyzc.lhy.mis.service.worksheet.IWorksheetService;
import com.xnyzc.lhy.resource.entity.worksheet.param.*;
import com.xnyzc.lhy.resource.entity.worksheet.param.heli.FromHeliWorksheetParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工单信息表 前端控制器
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-11-04
 */
@RestController
@Api(value = "工单信息表", tags = {"工单信息表"})
@RequestMapping("/worksheet")
public class OaSysWorksheetController {
    @Autowired
    IOaSysWorksheetService iOaSysWorksheetService;

    @Autowired
    IWorksheetService iWorksheetService;

    @Autowired
    private IOaSysWorksheetOperateService oaSysWorksheetOperateService;

    /**
     * 取消工单
     *
     * @param worksheetCancelOrderParam
     * @return
     */
    @ApiOperation(value = "取消工单", notes = "取消工单")
    @PostMapping(value = "/order/cancelOrder")
    public DiDiRv cancelOrder(@ApiParam(name =
            "worksheetCancelOrderParam", value = "取消订单") @RequestBody WorksheetCancelOrderParam worksheetCancelOrderParam) {
        if (StringUtils.isBlank(worksheetCancelOrderParam.getOrderId())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetCancelOrderParam,
                            0L,
                            "cancelOrder");

            // 取消订单
            DiDiRv didiRv = iWorksheetService.cancelOrder(worksheetCancelOrderParam);

            if (oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), didiRv);
            }

            return didiRv;
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 关闭工单
     *
     * @param worksheetCloseOrderParam
     * @return
     */
    @ApiOperation(value = "客服关单", notes = "客服关单")
    @PostMapping(value = "/order/getCloseOrder")
    public DiDiRv getCloseOrder(@ApiParam(name =
            "worksheetCancelOrderParam", value = "客服关单") @RequestBody WorksheetCloseOrderParam worksheetCloseOrderParam) {
        if (StringUtils.isBlank(worksheetCloseOrderParam.getOrder_id())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetCloseOrderParam,
                            0L,
                            "getCloseOrder");

            // 关闭订单
            DiDiRv didiRv =  iWorksheetService.getCloseOrder(worksheetCloseOrderParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), didiRv);
            }
            return didiRv;
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 订单改价接口
     *
     * @param changePriceParam
     * @return
     */
    @ApiOperation(value = "订单改价接口", notes = "订单改价接口")
    @PostMapping(value = "/order/getChangePrice")
    public DiDiRv getChangePrice(@ApiParam(name =
            "worksheetGetChangePriceParam", value = "订单改价接口") @RequestBody ChangePriceParam changePriceParam) {
        if (StringUtils.isBlank(changePriceParam.getOrder_id())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(changePriceParam,
                            0L,
                            "getChangePrice");

            // 订单改价
            DiDiRv didiRv = iWorksheetService.getChangePrice(changePriceParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), didiRv);
            }
            return didiRv;

        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 退款接口
     *
     * @param worksheetGetBillRefundParam
     * @return
     */
    @ApiOperation(value = "退款接口", notes = "退款接口")
    @PostMapping(value = "/order/getBillRefund")
    public DiDiRv getBillRefund(@ApiParam(name =
            "worksheetGetBillRefundParam", value = "退款接口") @RequestBody WorksheetGetBillRefundParam worksheetGetBillRefundParam) {
        if (StringUtils.isBlank(worksheetGetBillRefundParam.getOrder_id())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetGetBillRefundParam,
                            0L,
                            "getBillRefund");

            // 退款接口
            DiDiRv didiRv = iWorksheetService.getBillRefund(worksheetGetBillRefundParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), didiRv);
            }
            return didiRv;
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 免单接口
     *
     * @param worksheetFreeChargeParam
     * @return
     */
    @ApiOperation(value = "免单接口", notes = "免单接口")
    @PostMapping(value = "/order/getFreeCharge")
    public DiDiRv getFreeCharge(@ApiParam(name =
            "worksheetFreeChargeParam", value = "免单接口") @RequestBody WorksheetFreeChargeParam worksheetFreeChargeParam) {
        if (StringUtils.isBlank(worksheetFreeChargeParam.getOrderId())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetFreeChargeParam,
                            0L,
                            "getFreeCharge");

            // 免单接口
            DiDiRv didiRv = iWorksheetService.getFreeCharge(worksheetFreeChargeParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), didiRv);
            }
            return didiRv;
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 工单状态同步第三方（滴滴）接口
     *
     * @param worksheetUpdateWorksheetParam
     * @return
     */
    @ApiOperation(value = "工单状态同步第三方（滴滴）接口", notes = "工单状态同步第三方（滴滴）接口")
    @PostMapping(value = "/updateWorksheet")
    public WorksheetRv updateWorksheet(@ApiParam(name =
            "worksheetUpdateWorksheetParam", value = "工单状态同步第三方") @RequestBody WorksheetUpdateWorksheetParam worksheetUpdateWorksheetParam) {
        if (StringUtils.isBlank(worksheetUpdateWorksheetParam.getWorksheetId())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetUpdateWorksheetParam,
                            0L,
                            "updateWorksheet");

            // 工单状态同步第三方
            WorksheetRv worksheetRv = iWorksheetService.updateWorksheet(worksheetUpdateWorksheetParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                worksheetRv.setData("");
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }
            return worksheetRv;
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.updateWorksheetError);
        }
    }

    /**
     * 查询订单详情接口
     *
     * @param worksheetGetOrderPersonalDetailParam
     * @return
     */
    @ApiOperation(value = "查询订单详情接口", notes = "查询订单详情接口")
    @PostMapping(value = "/order/getOrderPersonalDetail")
    public WorksheetRv getOrderPersonalDetail(@ApiParam(name =
            "worksheetGetOrderPersonalDetailParam", value = "免单接口") @RequestBody WorksheetGetOrderPersonalDetailParam worksheetGetOrderPersonalDetailParam) {
        if (StringUtils.isBlank(worksheetGetOrderPersonalDetailParam.getOrderId())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {

            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetGetOrderPersonalDetailParam,
                            0L,
                            "getOrderPersonalDetail");

            // 工单状态同步第三方
            WorksheetRv worksheetRv = iWorksheetService.getOrderPersonalDetail(worksheetGetOrderPersonalDetailParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }
            return worksheetRv;
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 查询订单状态接口
     *
     * @param worksheetGetOrderPersonalStatusParam
     * @return
     */
    @ApiOperation(value = "查询订单状态接口", notes = "查询订单状态接口")
    @PostMapping(value = "/order/getOrderPersonalStatus")
    public WorksheetRv getOrderPersonalStatus(@ApiParam(name =
            "worksheetGetOrderPersonalStatusParam", value = "查询订单状态接口") @RequestBody WorksheetGetOrderPersonalStatusParam worksheetGetOrderPersonalStatusParam) {
        if (StringUtils.isBlank(worksheetGetOrderPersonalStatusParam.getOrderId())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetGetOrderPersonalStatusParam,
                            0L,
                            "getOrderPersonalStatus");

            // 查询订单状态接口
            WorksheetRv worksheetRv = iWorksheetService.getOrderPersonalStatus(worksheetGetOrderPersonalStatusParam);

            if (oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }
            return worksheetRv;
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 查询订单列表接口
     *
     * @param worksheetgetOrderPersonalListParam
     * @return
     */
    @ApiOperation(value = "查询订单详情列表", notes = "查询订单详情列表")
    @PostMapping(value = "/order/getOrderPersonalList")
    public WorksheetRv getOrderPersonalList(@ApiParam(name =
            "worksheetgetOrderPersonalListParam", value = "查询订单详情列表") @RequestBody WorksheetgetOrderPersonalListParam worksheetgetOrderPersonalListParam) {
        if (StringUtils.isBlank(worksheetgetOrderPersonalListParam.getCusPhone())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetgetOrderPersonalListParam,
                            0L,
                            "getOrderPersonalList");

            // 获取订单列表
            WorksheetRv worksheetRv = iWorksheetService.getOrderPersonalList(worksheetgetOrderPersonalListParam);

            if (oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }
            return worksheetRv;
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 客服标记司机违规
     *
     * @param worksheetetDriverPenaltyDetailParam
     * @return
     */
    @ApiOperation(value = "客服标记司机违规", notes = "客服标记司机违规")
    @PostMapping(value = "/driver/setDriverPenaltyDetail")
    public WorksheetRv setDriverPenaltyDetail(@ApiParam(name =
            "worksheetetDriverPenaltyDetailParam", value = "客服标记司机违规") @RequestBody WorksheetetDriverPenaltyDetailParam worksheetetDriverPenaltyDetailParam) {
        if (
                StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getDriverId())
                        || StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getOrderId())
                        || StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getDeregulationType())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }

        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetetDriverPenaltyDetailParam,
                            0L,
                            "setDriverPenaltyDetail");

            // 客服标记司机违规
            WorksheetRv worksheetRv = iWorksheetService.setDriverPenaltyDetail(worksheetetDriverPenaltyDetailParam);

            if (oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }
            return worksheetRv;
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.driverPenaltyFailed);
        }
    }

    /**
     * 司机信息接口
     *
     * @param worksheetetGetDriverDetailParam
     * @return
     */
    @ApiOperation(value = "司机信息接口", notes = "司机信息接口")
    @PostMapping(value = "/driver/getDriverDetail")
    public WorksheetRv getDriverDetail(@ApiParam(name =
            "worksheetetGetDriverDetailParam", value = "免单接口") @RequestBody WorksheetetGetDriverDetailParam worksheetetGetDriverDetailParam) {
        if (StringUtils.isBlank(worksheetetGetDriverDetailParam.getDriverPhone())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate =
                    oaSysWorksheetOperateService.createHeliWorksheetOperate(worksheetetGetDriverDetailParam,
                            0L,
                            "getDriverDetail");

            // 司机信息接口
            WorksheetRv worksheetRv = iWorksheetService.getDriverDetail(worksheetetGetDriverDetailParam);

            if (oaSysWorksheetOperate != null) {
                //更新接口操作记录
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }
            return worksheetRv;
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.getDriverFailed);
        }
    }


    /**
     * 查询工单通过合力工单id
     *getCloseOrder
     * @param heliWorksheetId
     * @return
     */
    @ApiOperation(value = "根据合力工单id查询工单信息", notes = "根据合力工单id查询工单信息")
    @GetMapping(value = "/queryWorksheetByHeliWorksheetId")
    public DiDiRv queryWorksheetByHeliWorksheetId(Long heliWorksheetId) {
        return iOaSysWorksheetService.queryWorksheetByHeliWorksheetId(heliWorksheetId);
    }

    /**
     * 查询工单通过DIDI工单id
     *
     * @param didiWorksheetId
     * @return
     */
    @ApiOperation(value = "通过didi工单查询工单信息", notes = "通过didi工单查询工单信息")
    @GetMapping(value = "/queryWorksheetByDiDiWorksheetId")
    public DiDiRv queryWorksheetByDiDiWorksheetId(Long didiWorksheetId) {
        return iOaSysWorksheetService.queryWorksheetByDiDiWorksheetId(didiWorksheetId);
    }

    /**
     * 创建工单
     *
     * @param fromHeliWorksheetParam
     * @return
     */
    @ApiOperation(value = "创建工单", notes = "创建工单")
    @PostMapping(value = "/createWorksheet")
    public WorksheetRv createWorksheet(@ApiParam(name =
            "fromHeliWorksheetParam", value = "创建工单") @RequestBody FromHeliWorksheetParam fromHeliWorksheetParam) {
        if (StringUtils.isBlank(fromHeliWorksheetParam.getWorksheet_id())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }

        try {
            //创建接口操作记录
            OaSysWorksheetOperate oaSysWorksheetOperate = oaSysWorksheetOperateService.createHeliWorksheetOperate(fromHeliWorksheetParam, 0L,"createWorksheet");

            // 创建工单接口
            WorksheetRv worksheetRv =  iWorksheetService.createWorksheet(fromHeliWorksheetParam);

            if(oaSysWorksheetOperate != null) {
                //更新接口操作记录
                worksheetRv.setData("");
                oaSysWorksheetOperateService.updateWorksheetOperate(oaSysWorksheetOperate.getId(), worksheetRv);
            }

            return worksheetRv;

        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(ex.getErrno());
        }
    }

    /**
     * 判断当前账户是否有电话条权限
     *
     * @return
     */
    @ApiOperation(value = "电话条权限判断", notes = "电话条权限判断")
    @GetMapping(value = "/hasPhoneBarAuth")
    public Rv hasPhoneBarAuth() {
        try {
            Boolean hasPhoneBar = iWorksheetService.hasPhoneBarAuth();
            if (hasPhoneBar) {
                return Rv.error(EErrorCode.success);
            } else {
                return Rv.error(EErrorCode.authErr);
            }
        } catch (Exception ex) {
            return Rv.error(EErrorCode.authErr);
        }
    }

    /**
     * 获取当前登录用户的软电话条的用户名和密码
     *
     * @return
     */
    @ApiOperation(value = "获取电话条用户信息", notes = "获取电话条用户信息")
    @GetMapping(value = "/getSoftPhoneUserInfo")
    public Rv getSoftPhoneUserInfo() {
        try {
            SoftPhoneUserInfoResult softPhoneUserInfoResult = iWorksheetService.getSoftPhoneUserInfo();
            if (softPhoneUserInfoResult != null) {
                return Rv.wrap(EErrorCode.success, softPhoneUserInfoResult);
            } else {
                return Rv.error(EErrorCode.authErr);
            }
        } catch (Exception ex) {
            return Rv.error(EErrorCode.authErr);
        }
    }

    /**
     * 获取电话条地址
     *
     * @return
     */
    @ApiOperation(value = "获取电话条地址", notes = "获取电话条地址")
    @GetMapping(value = "/getHeliPhoneUrl")
    public Rv getHeliPhoneUrl() {
        HeliPhoneUrlResult heliPhoneUrlResult = iWorksheetService.getHeliPhoneUrl();

        return Rv.wrap(EErrorCode.success, heliPhoneUrlResult);
    }
}
